package com.mobile.challenge.ui.report.unit.admin.daily

import android.os.Bundle
import androidx.lifecycle.Observer
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis.XAxisPosition
import com.github.mikephil.charting.formatter.ValueFormatter
import com.mobile.challenge.R
import com.mobile.challenge.databinding.FragmentUnitDailyReportBinding
import com.mobile.challenge.ui.report.adapter.IRefresh
import com.mobile.frame.mvvmframe.base.BaseFragment
import java.util.*


/**
 * @Author Touge
 * @Date 2020/9/16 21:24
 * @Description
 */
class AdminUnitDailyReportFragment : BaseFragment<AdminUnitDailyReportViewModel, FragmentUnitDailyReportBinding>()
        , IRefresh {

    companion object {
        fun newInstance(): AdminUnitDailyReportFragment {
            val args = Bundle()

            val fragment = AdminUnitDailyReportFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_unit_daily_report
    }

    override fun initData(savedInstanceState: Bundle?) {
        initBarChart()
        mViewModel.generateData()
        mViewModel.barData.observe(this, Observer { data ->
            mBinding.barChart.data = data
            mBinding.barChart.animateXY(1000, 1000)
        })
    }

    private fun initBarChart() {
        with(mBinding.barChart) {

            val description = Description()
            description.text = ""
            // 设置边距
//            setExtraOffsets(0f, 10f, 0f, 0f)

            // X 轴 显示位置
            xAxis.position = XAxisPosition.BOTTOM
            // 取消网格线
            xAxis.setDrawGridLines(false)
            // X 坐标居中
            xAxis.setCenterAxisLabels(true)
            // 设置 x 坐标轴 宽度
            xAxis.axisLineWidth = 2f
            // 设置 x轴 的刻度数量
            xAxis.labelCount = 5

            xAxis.valueFormatter = object : ValueFormatter() {

                override fun getFormattedValue(value: Float): String {
                    return if (value < 0F || value > 3F) {
                        ""
                    } else {
                        "Unit ${value.toInt() + 1}"
                    }
                }
            }

            // 隐藏右边 Y 轴
            axisRight.isEnabled = false
            axisLeft.axisMinimum = 0f

            // 图例
            legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP
            legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
            legend.orientation = Legend.LegendOrientation.VERTICAL
            legend.setDrawInside(true)
            legend.yOffset = 0f
            legend.xOffset = 10f
            legend.yEntrySpace = 0f
            legend.textSize = 8f
        }
    }

    override fun refresh(calendar: Calendar, period: Int) {
        if (mViewModel != null) {
            mViewModel.generateData()
        }
    }
}